package com.hibase.core.timezone.config;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.core.timezone.constant.TimezoneContant;
import com.hibase.core.timezone.util.TimezoneUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;

/**
 * TODO
 * 全局返回结果统一
 *
 * @author EDZ
 * @version 1.0
 * @date 2020/9/2 17:20
 */
@ControllerAdvice()
@Slf4j
public class TimezoneResponseBodyAdvice implements ResponseBodyAdvice<Object> {


    @Autowired
    private TimezoneUtil timezoneUtil;

    @Value("${hibase.timezone.database:8}")
    private Integer databaseTimezone;

    //是否开启时区
    @Value("${hibase.timezone.enable:true}")
    private Boolean enableTimezone;

    /**
     * 判断哪些方法需要beforeBodyWrite
     *
     * @param methodParameter
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return enableTimezone;
    }

    @Override
    public Object beforeBodyWrite(Object object, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if (object instanceof ResponseModel) {
            ResponseModel responseModel = (ResponseModel) object;
            // 当前用户时区
            String timeZone = StrUtil.removePrefix(timezoneUtil.getCurrentTimeZone(), TimezoneContant.PREFIX);
            log.debug("------------------------------------------------------");
            log.debug("处理返回体时区开始");
            handleTimeZone(responseModel, Integer.valueOf(timeZone));
            log.debug("处理返回体时区结束");
            log.debug("------------------------------------------------------");
            return responseModel;
        }
        return object;
    }

    /**
     * 处理时区
     *
     * @param responseModel
     */
    private void handleTimeZone(ResponseModel responseModel, Integer timeZone) {
        //pageinfo
        if (responseModel.getData() instanceof PageInfo) {
            timezoneUtil.handleTimeZoneByList(((PageInfo) responseModel.getData()).getList(), databaseTimezone, timeZone);
        }
        //List
        else if (responseModel.getData() instanceof List) {
            timezoneUtil.handleTimeZoneByList((List) responseModel.getData(), databaseTimezone, timeZone);
        }
        //详情
        else {
            timezoneUtil.handleTimeZoneByDetail(responseModel.getData(), databaseTimezone, timeZone);
        }
    }
}
